package com.insleton.admin.user.controller;

import com.insleton.admin.user.model.Reply;
import com.insleton.admin.user.model.User;
import com.vteba.web.filter.jwt.Claims;
import com.vteba.web.filter.jwt.TokenService;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;
import java.util.Iterator;

@RestController
@RequestMapping("/auth")
public class AuthController {

    private static final Logger LOGGER = LogManager.getLogger(AuthController.class);

    @Inject
    private TokenService tokenService;

    @RequestMapping("/login")
    public Reply<User> login(@RequestBody User user) {
        Claims claims = new Claims();
        claims.setAud("12223");
        claims.setIss("44444");
        claims.setJti("ghdf44");
        claims.setSub("sghnb");
        String token = tokenService.getToken(claims);
        user.setAccessToken(token);
        user.setCode(200);
        return Reply.success("success", user);
    }

    // @RequestMapping("/login")
    // public User login(@RequestBody User user) {
    //     Claims claims = new Claims();
    //     claims.setAud("12223");
    //     claims.setIss("44444");
    //     claims.setJti("ghdf44");
    //     claims.setSub("sghnb");
    //     String token = tokenService.getToken(claims);
    //     user.setAccessToken(token);
    //     return user;
    // }

    @RequestMapping("/refresh")
    public Reply<String> refresh(@RequestHeader("Authorization") String accessToken) {
        LOGGER.info("Refresh access token: [{}]", accessToken);
        Claims claims = new Claims();
        claims.setAud("12223");
        claims.setIss("44444");
        claims.setJti("ghdf44");
        claims.setSub("sghnb");
        String token = tokenService.getToken(claims);
        return Reply.success("success", token);
    }

    @RequestMapping("/logout")
    public Reply<String> logout(HttpServletRequest request) {
        Enumeration<String> headers = request.getHeaderNames();
        // Foreach not applicable to type 'java.util.Enumeration<java.lang.String>
        while (headers.hasMoreElements()) {
            String name = headers.nextElement();
            String value = request.getHeader(name);
            LOGGER.info("Logout header: [{}:{}]", name, value);
        }
        String accessToken = ""; // 路由重复，是因为这个登出错误，400.导致后续代码没有执行，导致前端路由错误
        LOGGER.info("Logout access token: [{}]", accessToken);
        return Reply.success("success");
    }

    @RequestMapping("/codes")
    public Reply<String[]> codes() {
        LOGGER.info("get access codes");
        String[] codes = new String[]{
                "AC_100100",
                "AC_100110",
                "AC_100120",
                "AC_100010"
        };
        return Reply.success("success", codes);
    }
}
